package com.shensi.adminCollect.controller.station;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.adminCollect.domain.station.dto.PhysicalCardOrderDto;
import com.shensi.adminCollect.domain.station.dto.PhysicalCardSecretKeyDto;
import com.shensi.adminCollect.domain.station.vo.PhysicalCardDualVo;
import com.shensi.adminCollect.service.station.IPhysicalCardOrderService;
import com.shensi.adminCollect.service.system.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;
@Api("订单信息")
@RestController
@Slf4j
@RequestMapping("/physicalCard/order")
public class PhysicalCardOrderController {

    @Autowired
    private IPhysicalCardOrderService physicalCardOrderService;

    @Autowired
    private ISysUserService userService;

    @Value("${physicalCard.secretKeyA}")
    private String SECRET_KEY_A;

    @Value("${domain.payName}")
    private String API_PAY_URL;

    /**实体卡消费*/
    @PostMapping("/consumption")
    @ApiOperation(value = "实体卡消费",response = PhysicalCardSecretKeyDto.class)
    public AjaxResult consumption(@RequestBody PhysicalCardOrderDto physicalCardOrderDto){
        if(Objects.isNull(physicalCardOrderDto) || StringUtils.isEmpty(physicalCardOrderDto.getPhysicalCardNo())){
            return AjaxResult.error("请求参数异常");
        }
        physicalCardOrderDto.setPhysicalCardNo(com.shensi.adminCollect.common.utils.StringUtils.trim(physicalCardOrderDto.getPhysicalCardNo()));
        log.info("实体卡消费 请求参数：[{}]", JSONObject.toJSONString(physicalCardOrderDto));
        if(Objects.isNull(physicalCardOrderDto.getAmount())){
            return AjaxResult.error("请输入消费金额");
        }
        if(Objects.isNull(physicalCardOrderDto.getConsumptionStationId())){
            return AjaxResult.error("请输入油站");
        }
        if(StringUtils.isEmpty(physicalCardOrderDto.getOilGun())){
            return AjaxResult.error("请输入油枪号");
        }
        if(StringUtils.isEmpty(physicalCardOrderDto.getOilPersonnelId())){
            return AjaxResult.error("请输入加油员");
        }

        HttpResponse response = HttpUtil.createPost(API_PAY_URL+"/apipay/physicalCard/consumption")
                .body(JSONObject.toJSONString(physicalCardOrderDto)).timeout(5000).execute();
        JSONObject jsonObject = JSON.parseObject(response.body());
        JSONObject data = jsonObject.getJSONObject("data");
        log.info("admin 实体卡消费：[{}]",data.toJSONString());
        PhysicalCardDualVo physicalCardDualVo = new PhysicalCardDualVo();
        physicalCardDualVo.setPhysicalCardNo(data.getString("physicalCardNo"));
        physicalCardDualVo.setSecretKeyA(SECRET_KEY_A);
        return AjaxResult.success(data);
    }

    /**
     * 根据 员工 查询油枪号
     * */
    @GetMapping("/getGun")
    @ApiOperation(value="根据 员工 查询油枪号",response = List.class)
    public AjaxResult getGun(@RequestParam("userId") Long userId){
        if(Objects.isNull(userId)){
            return AjaxResult.error("请求参数异常");
        }
        SysUser sysUser = userService.selectUserById(userId);
        List<String> list = Arrays.stream(sysUser.getGun().split(",")).collect(Collectors.toList());
        return AjaxResult.success(list);
    }
}
